declare let eManager;
const {ccclass, property} = cc._decorator;

const fenDiPos = [
    cc.v2(132,62),
    cc.v2(13,-155),
    cc.v2(273,-157)
];
//2-1 抹粉底
@ccclass
export default class MoFenDi extends cc.Component {
    @property
    stepName = "";
    //粉底盒 
    @property(cc.Node)
    fenDiHe: cc.Node = null;
    //粉底碰撞盒
    @property(cc.Node)
    fenDiHePeng: cc.Node = null;
    //粉底印
    @property(cc.Node)
    fenDiYin: cc.Node[] = [];

    //抹完的粉底印
    @property(cc.Node)
    fenDiDone: cc.Node[] = [];

    //音效
    @property({type:cc.AudioClip})
    moFenDi: cc.AudioClip = null;

    _startPos = cc.v3(230,-420);
    _isPlay = false;
    _done_list = [];//抹完的粉底
    initData(){
        this._isPlay = false;
        this._done_list = [];
        this.fenDiHe.active = true;
        this.fenDiHe.position = this._startPos;
        for (let index = 0; index < this.fenDiYin.length; index++) {
            this.fenDiYin[index].active = true;
            this.fenDiDone[index].active = false;
        }
        eManager.emit("REFRESH_STEP_NAME", this.stepName);
        eManager.emit("HZ_REFRESH_ICON_TIPS", "step_2_1");
    };
    
    _touchStart(event){
        if(this._isPlay)
            return;
        if (this._done_list.length >= 3)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.fenDiHe.parent.convertToNodeSpaceAR(worldPoint);
        this.fenDiHe.x = localPoint.x;
        this.fenDiHe.y = localPoint.y;
    };

    _touchMove(event:any){
        if(this._isPlay)
            return;
        if(this._done_list.length >= 3)
            return;
        // let delta = event.touch.getDelta();
        // this.fenDiHe.x += delta.x;
        // this.fenDiHe.y += delta.y;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.fenDiHe.parent.convertToNodeSpaceAR(worldPoint);
        this.fenDiHe.x = localPoint.x;
        this.fenDiHe.y = localPoint.y;
        let zhen = this.fenDiHePeng.getBoundingBoxToWorld();
        for (let index = 0; index < this.fenDiYin.length; index++) {
            let dou = this.fenDiYin[index];
            let douBox = dou.getBoundingBoxToWorld();
            if(zhen.intersects(douBox)){
                this.playFenDiAni(index);
                break;
            }
        }
    };

    //抹粉
    playFenDiAni(index: number) {
        if (this._done_list.indexOf(index) == -1) {
            this._done_list.push(index);
            let moveto = cc.moveTo(0.2, fenDiPos[index]);
            this.fenDiHe.runAction(moveto);
            this.fenDiYin[index].active = false;
            let spine = this.fenDiHe.getComponent(sp.Skeleton);
            if (spine) {
                cc.audioEngine.play(this.moFenDi, false, 1);
                this._isPlay = true;
                spine.setAnimation(0,"animation",false);
                spine.setCompleteListener(() => {
                    this._isPlay = false;
                    this.fenDiDone[index].active = true;
                    if (this._done_list.length >= 3) {//都抹完了
                        this.fenDiHe.active = false;
                        eManager.emit("DONE_POINT", 1);
                        eManager.emit("MINI_DONE_POINT", 3);
                        eManager.emit("HZ_MAKE_DOT", {
                            makeup_close:2,
                            makeup_step:this.stepName
                        });
                    }
                });
            }
        }
    };

    setFenDiShow(isShow:boolean){
        for (let index = 0; index < this.fenDiYin.length; index++) {
            this.fenDiYin[index].active = isShow == true;
        }
    };

}
